package org.stone.project.securityInterface.utils;

import org.stone.project.securityInterface.pojo.dto.UserInfoDto;

import java.util.List;

/**
 * 权限校验工具
 * 所有校验只能返回boolean值,参数最后一个必须是用户信息
 */
public class PermissionsCheckUtil {
    /**
     * 包含角色
     * @param roleKeys
     * @param userInfoDto
     * @return
     */
    public static boolean includeRole(List<String> roleKeys, UserInfoDto userInfoDto) {
        return roleKeys.stream()
                .anyMatch(ele -> ele.equals(userInfoDto.getLoginRoleKey()));
    }

    /**
     * 不包含角色
     * @param roleKeys
     * @param userInfoDto
     * @return
     */
    public static boolean notContain(List<String> roleKeys, UserInfoDto userInfoDto) {
        return !(roleKeys.stream()
                .anyMatch(ele -> ele.equals(userInfoDto.getLoginRoleKey())));
    }


    /**
     * 包含权限
     * @param permissions
     * @param userInfoDto
     * @return
     */
    public static boolean includePermission(List<String> permissions, UserInfoDto userInfoDto) {
        return permissions.stream()
                .anyMatch(ele -> userInfoDto.getPermissions().contains(ele));
    }

    /**
     * 不包含权限
     * @param permissions
     * @param userInfoDto
     * @return
     */
    public static boolean noContainPermission(List<String> permissions, UserInfoDto userInfoDto) {
        return !(permissions.stream()
                .anyMatch(ele -> userInfoDto.getPermissions().contains(ele)));
    }
}
